package common.dao.Impl;

import cn.hutool.core.util.StrUtil;
import common.dao.DeptDao;
import common.domain.Dept;
import common.domain.Role;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl implements DeptDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(Dept dept) {
        String sql = "insert into dept\n" +
                "        (dept_id,dept_name,dept_info) \n" +
                "values(?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,dept.getDept_id());
            psmt.setString(2,dept.getDept_name());
            psmt.setString(3,dept.getDept_info());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(Dept dept) {
        String sql = "update dept set";
        List<Object> param = new ArrayList<>();
        if (dept == null){
            return;
        }else{
            if(dept.getDept_id() != null){
                sql += " depet_id = ?,";
                param.add(dept.getDept_id());
            }
            if(dept.getDept_name() != null){
                sql += " dept_name = ?,";
                param.add(dept.getDept_name());
            }
            if(dept.getDept_info() != null){
                sql += " dept_info = ?,";
                param.add(dept.getDept_info());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where depet_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,dept.getDept_id());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long dept_id) {
        String sql = "delete from dept where dept_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,dept_id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public Dept selectById(Long dept_id) {
        String sql = "select * from dept where dept_id = ?";
        Dept dept = new Dept();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,dept_id);
            rs = psmt.executeQuery();
            while (rs.next()){
                dept.setDept_id(rs.getLong(1));
                dept.setDept_name(rs.getString(2));
                dept.setDept_info(rs.getString(3));

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return dept;    }

    @Override
    public List<Dept> selectList(Dept dept) {
        String sql = "select * from dept where 1=1";
        List<Dept> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (dept == null){
            sql = "select * from dept";
        }else{
            if(dept.getDept_id() != null){
                sql += " and dept_id = ?";
                param.add(dept.getDept_id());
            }
            if(dept.getDept_name() != null){
                sql += " and dept_name like \"%\"?\"%\"";
                param.add(dept.getDept_name());
            }
            if(dept.getDept_info() != null){
                sql += " and dept_info = ?";
                param.add(dept.getDept_info());
            }

        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                Dept dept1 = new Dept();
                dept1.setDept_id(rs.getLong(1));
                dept1.setDept_name(rs.getString(2));
                dept1.setDept_info(rs.getString(3));

                list.add(dept1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
